package com.test.langchain4j.service;

import com.fasterxml.jackson.databind.JsonNode;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/8/3 17:23
 * @modifiedBy:
 * @version: 1.0
 */
@Slf4j
@Component
public class WeatherHandler {
  private final WeatherService weatherService;

  public WeatherHandler(WeatherService weatherService) {
    this.weatherService = weatherService;
  }

  @Tool("根据用户提供的城市信息查询当前天气")
  public String handle(@P("城市编码") String cityCode) {
    String weatherInfo = "查询城市编码：" + cityCode + " 的天气信息";
    log.info(weatherInfo);
    String stringWeather;
    try {
      JsonNode weatherV2 = weatherService.getWeatherV2(cityCode);
      stringWeather = weatherV2.toPrettyString();
      log.info(stringWeather);
    } catch (Exception e) {
      log.error("查询天气信息失败", e);
      stringWeather = "查询天气信息失败：" + e.getMessage();
    }
    return stringWeather;
  }
}
